package com.qilu.pe.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.TestStatus;
import com.qilu.pe.dao.http.APIRetrofit;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 检验状态fragment
public class TestStatusFragment extends BaseFragment {
    private RecyclerView rcv_list;// 化验单列表
    private TextView empty_view;

    private TestListAdapter adapter;
    private List<TestStatus> testList = new ArrayList<>();// 化验单列表数据

    private String status = "";// 区分检验状态

    public TestStatusFragment() {

    }

    public static TestStatusFragment newInstance(String status) {
        TestStatusFragment fragment = new TestStatusFragment();
        Bundle args = new Bundle();
        args.putString("status", status);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status = getArguments().getString("status");
        adapter = new TestListAdapter(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_test_status, container, false);
        rcv_list = v.findViewById(R.id.rcv_list);
        empty_view = v.findViewById(R.id.empty_view);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        adapter.setDataList(testList);

        hideProgress();
        reqTestStatusList();
        showProgress();

        return v;
    }

    // 获取检验状态列表
    private Disposable reqTestStatusList() {
        return APIRetrofit.getDefault().reqTestStatusList(Global.HEADER, status)
                .compose(RxUtil.<BaseResult2<List<TestStatus>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<TestStatus>>>() {
                    @Override
                    public void accept(BaseResult2<List<TestStatus>> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {

                        } else {

                        }
                        testList = listBaseResult2.getData();
                        adapter.setDataList(testList);
                        if (isDataEmpty(testList)) {
                            empty_view.setVisibility(View.VISIBLE);
                        } else {
                            empty_view.setVisibility(View.GONE);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                        empty_view.setVisibility(View.VISIBLE);
                    }
                });
    }

    private class TestListAdapter extends ListBaseAdapter<TestStatus> {

        public TestListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_test_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_time = holder.getView(R.id.tv_time);
            TextView tv_no = holder.getView(R.id.tv_no);
            TextView tv_order = holder.getView(R.id.tv_order);
            CardView cv_item = holder.getView(R.id.cv_item);

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) cv_item.getLayoutParams();
            if (position == 0) {
                params.topMargin = SizeUtils.dp2px(10);
                params.bottomMargin = SizeUtils.dp2px(5);
            } else if (position == mDataList.size() - 1) {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(10);
            } else {
                params.topMargin = SizeUtils.dp2px(5);
                params.bottomMargin = SizeUtils.dp2px(5);
            }

            TestStatus status = mDataList.get(position);
            tv_name.setText(status.getSample());
            tv_time.setText(status.getDay() + "天");
            tv_no.setText(status.getOrder_id());

            tv_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ToastUtils.showShort("查看订单");
                }
            });
        }
    }
}